import { Provide, App, Inject, Config } from '@midwayjs/core';
import { GraphType } from '../entity/graphType.entity';
import { Graph } from '../entity/graph.entity';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, Like, } from 'typeorm';
import { addGraphTypeDto, updateGraphTypeDto, batchUpdateGraphTypeDto } from '../dto/GraphTypeDto';
import { v4 as uuidv4 } from 'uuid';
import { where } from 'sequelize';
import { group } from 'console';

@Provide()
export class GraphTypeService {
    @InjectEntityModel(GraphType)
    GraphTypeModel: Repository<GraphType>;

    @InjectEntityModel(Graph)
    graphModel: Repository<Graph>;

    async addGraphType(params: addGraphTypeDto) {
        let graphType = new GraphType();
        graphType.id = uuidv4();
        graphType.name = params.name
        graphType.desc = params.desc
        let result = this.GraphTypeModel.save(graphType)
        if (result) {
            return {
                succes: true,
                data: result,
                message: '成功',
            };
        } else {
            return {
                succes: true,
                message: '新增算法类型失败',
            };
        }

    }

    async getAllGraphTypes() {
        try {

            let graphTypes = await this.GraphTypeModel.find({
                order: {
                    createDate: 'DESC'
                }
            });
            return {
                success: true,
                data: graphTypes,
                message: '获取所有算法类型成功',
            };
        } catch (error) {
            return {
                success: false,
                message: '获取所有算法类型失败: ' + error.message,
            };
        }
    }

    async deleteGraphType(id: string) {
        try {
            let graphType = await this.GraphTypeModel.findOne({ where: { id } });
            if (!graphType) {
                return {
                    success: false,
                    message: '要删除的流程类型不存在'
                };
            }
            let relatedGraphs = await this.graphModel.find({ where: { type: id } });
            if (relatedGraphs.length > 0) {
                return {
                    success: false,
                    message: '类型下还存在算法,无法删除'
                };
            }

            await this.GraphTypeModel.remove(graphType);
            return {
                success: true,
                message: '删除流程类型成功'
            };
        } catch (error) {
            return {
                success: false,
                message: '删除流程类型失败: ' + error.message
            };
        }
    }
    async updateGraphType(id: string, params: updateGraphTypeDto) {
        try {
            let graphType = await this.GraphTypeModel.findOne({ where: { id } });
            if (!graphType) {
                return {
                    success: false,
                    message: '要更新的算法类型不存在'
                };
            }
            graphType.name = params.name;
            graphType.desc = params.desc;
            let result = await this.GraphTypeModel.save(graphType);
            return {
                success: true,
                data: result,
                message: '更新算法类型成功'
            };
        } catch (error) {
            return {
                success: false,
                message: '更新算法类型失败: ' + error.message
            };
        }
    }

    async batchAddGraphType(id: string, params: batchUpdateGraphTypeDto) {
        try {
            const { processes } = params;

            // 使用 Promise.all 并行更新多个图表的类型
            await Promise.all(
                processes.map(async (graphId) => {
                    const graph = await this.graphModel.findOne({
                        where: {
                            id: Number(graphId),
                        },
                    });

                    if (graph) {
                        graph.type = id;
                        await this.graphModel.save(graph)
                    } else {
                        console.error(`Graph with ID ${graphId} not found.`);
                    }
                })
            );

            return {
                success: true,
                message: '更新算法类型成功',
            };
        } catch (error) {
            console.error('Error in batchAddGraphType:', error);
            return {
                success: false,
                message: '更新算法类型失败',
            };
        }
    }
    async removeGraphType(id: number) {
        try {
            const graph = await this.graphModel.findOne({
                where: {
                    id: id,
                },
            });

            if (graph) {
                graph.type = '';
                await this.graphModel.save(graph)
                return {
                    success: true,
                    message: '算法类型删除成功',
                };
            } else {
                console.error(`Graph with ID ${id} not found.`);
                return {
                    success: false,
                    message: '算法流程不存在',
                };
            }
        } catch (error) {
            console.error('Error in removeGraphType:', error);
            return {
                success: false,
                message: '删除算法类型发生错误',
            };
        }
    }
}

